package com.valuegen.generater;

import java.text.DecimalFormat;
import java.util.Random;

import android.content.Context;

import com.valuegen.R;

/**
 * @author
 * @version 创建时间：2012-4-14 下午03:18:33 类说明
 */
public class RemarkGenerater {

	private final static DecimalFormat decimalFormat = new DecimalFormat();

	private static Context mContext;
	static {
		decimalFormat.setMaximumFractionDigits(2);
	}

	public static Remark getRemark(String name) throws Exception {

		if (name.length() < 2) {

			throw new Exception(mContext.getString(R.string.warning));
		}
		for (String impName : IMPORTANT_NAMES) {
			if (impName.equals(name)) {
				return TRASURE;
			}
		}
		double value = getGeneratedLongValue(name);

		value %= 1E10;
		int commentId;
		double perValue = value / name.length();
		if (perValue > 1E8) {
			if (value > 1E9) {
				commentId = 5;
				if (value % 24 < 12) {
					commentId = 1;
					value = value % 1E3;
				}
			} else {
				commentId = 4;
			}
		} else {

			if (value < 1) {
				commentId = 0;
			} else if (value < 1E3) {
				commentId = 1;
			} else if (value < 1E6) {
				commentId = 2;
			} else {
				commentId = 3;
			}

		}
		String valueString = decimalFormat.format(value);

		String[] comments = mContext.getString(COMMENTS[commentId])
				.split("\\|");
		String commant = comments[name.length() % comments.length];
		return new Remark(valueString+ mContext.getString(R.string.currency_unit), commant);

	}

	public static double getGeneratedLongValue(String data) {
		byte[] bytes = data.getBytes();
		double d = getValue(bytes) / 1E7;
		return d * d;
	}

	private static int getOneBitCount(byte data) {
		int count = 0;
		while (data != 0) {
			count++;
			data &= data - 1;
		}
		return count;
	}

	private static long getValue(byte[] rawContent) {
		// 数组重排，
		int rawContentLength = rawContent.length;
		byte curByte, lowBit, highBit, lowBitShuff, layoutByte = 0;
		for (int i = 0; i < rawContentLength; i++) {
			curByte = rawContent[i];
			lowBit = (byte) (curByte & 0x0f);
			lowBitShuff = (byte) ((lowBit << 2) & 0x30);
			highBit = (byte) (lowBitShuff + lowBit << 6);
			rawContent[i] = (byte) (lowBit | highBit);

		}
		layoutByte = (byte) (rawContent[0] ^ rawContent[rawContentLength - 1]);

		if (getOneBitCount(layoutByte) < 4) {
			layoutByte |= (byte) 0xf000;
		} else {
			layoutByte &= (byte) 0x0fff;
		}

		int fillLength = rawContentLength;
		long finalValue = 0;
		long value = 0;
		int scanIndex = 8;
		byte layoutPointer = (byte) 0x80;

		while (scanIndex > 0) {
			if ((layoutPointer & layoutByte) != 0) {
				for (int i = 0; i < scanIndex; i++) {
					long valueToSet = ((long) rawContent[i % fillLength]) & 0x000000ff;
					value ^= valueToSet;
					value <<= 4;
				}
				finalValue ^= value;
			}
			value = 0;
			scanIndex--;
			layoutPointer >>= 1;
			layoutPointer &= 0x7f;
		}
		finalValue &= 0x7fffffffffffffffl;
		return finalValue;
	}

	private static Remark TRASURE;

	private final static int[] COMMENTS = new int[] { R.string.level1,
			R.string.level2, R.string.level3, R.string.level4, R.string.level5,
			R.string.level6, R.string.treasure };

	private final static String[] IMPORTANT_NAMES = new String[] { "刘畅", "王安辰",
			"李佳琪", "李英丽" };

	public static void setContext(Context context) {
		mContext = context;
		String[] splitComments = mContext.getString(R.string.treasure).split(
				"\\|");
		int nextInt = (new Random(System.currentTimeMillis())).nextInt(splitComments.length);
		TRASURE = new Remark(mContext.getString(R.string.uncountable),
				splitComments[nextInt]);
	}
}
